<?php
namespace app\business\controller;
use app\business\model\BusinessClassify;
use app\business\model\BusinessLogin;
use app\business\model\BusinessMenu;
use think\Controller;
use app\business\controller\Base;
use think\Session;

class Activity extends Base
{
    public function coupon()
    {
        $a = input('post.');
        if (!empty($a)) {
            $data = db('coupon')->where('name','like','%'.$a['search'].'%')->select();
        }else{
            $username = Session::get('username');

            $count = BusinessLogin::where('username',$username)->find();

            $num=$count['business_id'];

            $data = db('coupon')->where('business_id',$num)->select();
        }
        $this->assign('data',$data);

        return $this->fetch();
    }
    public function delete($id)
    {

        $static = db('coupon')->delete($id);

        if ($static) {
            return $this->success('删除成功','business/activity/coupon');
        }
    }

    public function create()
    {
        return $this->fetch();
    }

    public function do_create()
    {


        $data = input('post.');
        
        if (isset($data['id'])) {
           if (empty($data['name']) || empty($data['full']) || empty($data['reduce']) || empty($data['number'])) {

           return $this->error('请详细填写！'); 
        }
        $id = $data['id'];

        unset($data['id']);

        db('coupon')->where('id',$id)->update($data);

       
        return $this->redirect('business/activity/coupon');die;
       
        }
        if (empty($data['name']) || empty($data['full']) || empty($data['reduce']) || empty($data['number'])) {

           return $this->error('请详细填写！'); 
        }
        $data['post_time'] = date('Y-m-d H:i:s');
        $username = Session::get('username');

        $count = BusinessLogin::where('username',$username)->find();

        $num=$count['business_id'];
        $data['business_id'] = $num;
        $static = db('coupon')->insert($data);

        if ($static) {
           return $this->redirect('business/activity/coupon');
        }
    }

    public function update($id)
    {
        $data = db('coupon')->find($id);

        $this->assign('data',$data);

        return $this->fetch('create');
    }

}